//
//  NoticeListViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/5/23.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "NoticeListViewController.h"
#import "NoticeListCell.h"
#import "NoticeSettingViewController.h"
#import "ClockViewModel.h"
@interface NoticeListViewController ()<UITableViewDelegate,UITableViewDataSource,NoticeListCellDelegate>{
    
}
@property(nonatomic,weak)IBOutlet UITableView *mainTableView;
@property (nonatomic, strong) ClockViewModel *viewModel;
@end

@implementation NoticeListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"提醒设置";
    [self setNavRightBtnForImage:[UIImage imageNamed:@"icon_add"]];
    [self configTableView];
}
-(void)configTableView{
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.tableFooterView = [UIView new];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"NoticeListCell" bundle:nil] forCellReuseIdentifier:@"NoticeListCell"];
    self.mainTableView.showsVerticalScrollIndicator = NO;
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.mainTableView.backgroundColor = [UIColor clearColor];
}
-(void)rightbuttonPressed:(id)sender{
    NoticeSettingViewController *noticeSetting = [[NoticeSettingViewController alloc]init];
    noticeSetting.block = ^(ClockModel * _Nonnull model) {
        [self.viewModel addClockModel:model];
        [self.mainTableView reloadData];
    };
    [self.rt_navigationController pushViewController:noticeSetting animated:YES complete:nil];
}
#pragma mark - UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.clockData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 92;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NoticeListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoticeListCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    if (indexPath.row==0) {
        [cell.bgView setImage:[[UIImage imageNamed:@"topBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"topBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"topBg"].size.height/2]];
        cell.line.hidden = NO;
    }else if (indexPath.row<2-1){
        [cell.bgView setImage:[[UIImage imageNamed:@"middleBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"middleBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"middleBg"].size.height/2]];
        cell.line.hidden = NO;
    }else{
        [cell.bgView setImage:[[UIImage imageNamed:@"bottomBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"bottomBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"bottomBg"].size.height/2]];
        cell.line.hidden = YES;
    }
    cell.model = self.viewModel.clockData[indexPath.row];
    cell.indexPath = indexPath;
    cell.delegate = self;
    return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.viewModel removeClockAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}
- (void)alarmCell:(NoticeListCell *)cell switch:(UISwitch *)switchControl didSelectedAtIndexpath:(NSIndexPath *)indexpath {
    [self.viewModel changeClockSwitchIsOn:switchControl.isOn WithModel:self.viewModel.clockData[indexpath.row]];
}
#pragma mark -- getter

- (ClockViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [ClockViewModel readData];
    }
    return _viewModel;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
